package com.zzh.oauth.config;

import com.zzh.oauth.intergration.IntegrationAuthenticationContext;
import com.zzh.oauth.intergration.IntegrationAuthenticationEntity;
import com.zzh.oauth.intergration.authenticator.IntegrationAuthenticator;
import com.zzh.oauth.intergration.pojo.UserPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 加载用户信息
 */
@Service
public class UserService implements UserDetailsService {
    private List<User> userList;
    @Autowired
    private PasswordEncoder passwordEncoder;
    private List<IntegrationAuthenticator> authenticators;

    @Autowired(required = false)
    public void setIntegrationAuthenticators(List<IntegrationAuthenticator> authenticators) {
        this.authenticators = authenticators;
    }

    /**
     * 这个写法有问题，password会被清空
     *
     * @param s
     * @return
     * @throws UsernameNotFoundException
     */
  /*  @PostConstruct
    public void init(){
        String password = passwordEncoder.encode("123456");
        userList=new ArrayList<>();
        userList.add(new User("admin", password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,client")));
        userList.add(new User("andy", password, AuthorityUtils.commaSeparatedStringToAuthorityList("client")));
        userList.add(new User("mark", password, AuthorityUtils.commaSeparatedStringToAuthorityList("client")));


    }*/
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

      /*  String password = passwordEncoder.encode("123");
        userList = new ArrayList<>();
        userList.add(new User("admin", password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,client")));
        userList.add(new User("andy", password, AuthorityUtils.commaSeparatedStringToAuthorityList("client")));
        userList.add(new User("mark", password, AuthorityUtils.commaSeparatedStringToAuthorityList("client")));


        List<User> findUserList = userList.stream().filter(user -> Objects.equals(user.getUsername(), s)).collect(Collectors.toList());
        if (!findUserList.isEmpty()) {
            return findUserList.get(0);
        } else {
            throw new UsernameNotFoundException("用户或密码错误");
        }*/
        IntegrationAuthenticationEntity entity = IntegrationAuthenticationContext.get();
        if (entity == null){
            entity = new IntegrationAuthenticationEntity();
        }
        UserPojo pojo = this.authenticate(entity);
        if (pojo == null){
            throw new OAuth2Exception("此账号不存在！");
        }
        User user = new User(pojo.getName(),passwordEncoder.encode(entity.getAuthParameter("password")), AuthorityUtils.commaSeparatedStringToAuthorityList("ROOT_USER"));
        return user;

    }
    private UserPojo authenticate(IntegrationAuthenticationEntity entity) {
        if (this.authenticators != null) {
            for (IntegrationAuthenticator authenticator : authenticators) {
                if (authenticator.support(entity)) {
                    return authenticator.authenticate(entity);
                }
            }
        }
        throw new OAuth2Exception("无效的auth_type参数值！");
    }
}
